package com.chinags.system.report.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.chinags.common.core.domain.Ztree;
import com.chinags.framework.util.ShiroUtils;
import com.chinags.system.report.domain.TJcFlzddw;
import com.chinags.system.report.service.ITJcFlzddwService;
import com.chinags.system.testingfacility.domain.TJcJcjg;
import com.chinags.system.testingfacility.service.ITJcJcjgService;
import com.chinags.web.controller.demo.domain.UserOperateModel;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.chinags.common.annotation.Log;
import com.chinags.common.enums.BusinessType;
import com.chinags.common.core.controller.BaseController;
import com.chinags.common.core.page.TableDataInfo;
import com.chinags.common.core.domain.AjaxResult;
import com.chinags.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 防雷重点单位 信息操作处理
 * 
 * @author liut
 * @date 2019-06-20
 */
@Controller
@RequestMapping("/system/tJcFlzddw")
public class TJcFlzddwController extends BaseController
{
    private String prefix = "system/report/tJcFlzddw";
    private String prefix1 ="system/report/tJcJcbg";


	@Autowired
	private ITJcFlzddwService tJcFlzddwService;
	@Autowired
	private ITJcJcjgService tJcJcjgService;
	
	@RequiresPermissions("system:tJcFlzddw:view")
	@GetMapping()
	public String tJcFlzddw()
	{
	    return prefix + "/tJcFlzddw";
	}
	
	/**
	 * 查询防雷重点单位列表
	 */
	@RequiresPermissions("system:tJcFlzddw:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(TJcFlzddw tJcFlzddw)
	{
		startPage();
        List<TJcFlzddw> list = tJcFlzddwService.selectTJcFlzddwList(tJcFlzddw);
		return getDataTable(list);
	}


	
	
	/**
	 * 导出防雷重点单位列表
	 */
	@RequiresPermissions("system:tJcFlzddw:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TJcFlzddw tJcFlzddw)
    {
    	List<TJcFlzddw> list = tJcFlzddwService.selectTJcFlzddwList(tJcFlzddw);
        ExcelUtil<TJcFlzddw> util = new ExcelUtil<TJcFlzddw>(TJcFlzddw.class);
        return util.exportExcel(list, "tJcFlzddw");
    }
	
	/**
	 * 新增防雷重点单位
	 */
	@GetMapping("/add")
	public String add()
	{


	    return prefix + "/add";
	}
	
	/**
	 * 新增保存防雷重点单位
	 */
	@RequiresPermissions("system:tJcFlzddw:add")
	@Log(title = "防雷重点单位", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(TJcFlzddw tJcFlzddw)
	{
		String administrativeRegion = tJcFlzddw.getAdministrativeRegion();
		int index = administrativeRegion.indexOf('/');
		String province  = administrativeRegion.substring(0,index);
		tJcFlzddw.setProvince(province);
		String[] words = administrativeRegion.split("/");
		for(int i = 0; i<words.length-1;i++){
			String city = words[1];
			String county = words[2];
			tJcFlzddw.setCity(city);
			tJcFlzddw.setCounty(county);
		}

		return tooAjax(tJcFlzddwService.insertTJcFlzddw(tJcFlzddw));
	}

	/**
	 * 修改防雷重点单位
	 */
	@GetMapping("/edit/{thunderUnitId}")
	public String edit(@PathVariable("thunderUnitId") Long thunderUnitId, ModelMap mmap)
	{
		TJcFlzddw tJcFlzddw = tJcFlzddwService.selectTJcFlzddwById(thunderUnitId);
		mmap.put("tJcFlzddw", tJcFlzddw);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存防雷重点单位
	 */
	@RequiresPermissions("system:tJcFlzddw:edit")
	@Log(title = "防雷重点单位", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(TJcFlzddw tJcFlzddw)
	{		
		return toAjax(tJcFlzddwService.updateTJcFlzddw(tJcFlzddw));
	}
	
	/**
	 * 删除防雷重点单位
	 */
	@RequiresPermissions("system:tJcFlzddw:remove")
	@Log(title = "防雷重点单位", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(tJcFlzddwService.deleteTJcFlzddwByIds(ids));
	}
	/***
	 * 导入防雷单位
	 */

	@RequiresPermissions("system:tJcFlzddw:import")
	@PostMapping("/importData")
	@ResponseBody
	public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
	{
		ExcelUtil<TJcFlzddw> util = new ExcelUtil<TJcFlzddw>(TJcFlzddw.class);
		List<TJcFlzddw> tJcFlzddwList = util.importExcel(file.getInputStream());
		String operName = ShiroUtils.getSysUser().getLoginName();
		String message = tJcFlzddwService.importUser(tJcFlzddwList, updateSupport, operName);
		return AjaxResult.success(message);
	}
	/**
	 * 下载模板
	 *
	 * */
	@RequiresPermissions("system:tJcFlzddw:view")
	@GetMapping("/importTemplate")
	@ResponseBody
	public AjaxResult importTemplate()
	{
		ExcelUtil<TJcFlzddw> util = new ExcelUtil<TJcFlzddw>(TJcFlzddw.class);
		return util.importTemplateExcel("防雷单位数据");
	}

	/**
	 * 查看详细
	 */
	@GetMapping("/detail/{thunderUnitId}")
	public String detail(@PathVariable("thunderUnitId") Long thunderUnitId, ModelMap mmap)
	{
		mmap.put("name", "tJcFlzddw");
		mmap.put("tJcFlzddw", tJcFlzddwService.selectJobLogById(thunderUnitId));
		return prefix + "/detail";
	}
//	/**
//	 * 加载部门列表树
//	 */
//	@GetMapping("/treeData")
//	@ResponseBody
//	public List<Ztree> treeData()
//	{
//		List<Ztree> ztrees = tjcflz.selecitDeptTree(new TJcFlzddwCount());
//		return ztrees;
//	}
	/**
	 * 根据社会统一代码查找所有的社会统一代码
	 * */
	@PostMapping("/judge")
	@ResponseBody
	public int judge(Long creditCode){

		return tJcFlzddwService.selectCreadConde(creditCode);
	}
    /**
     * 根据单位名称查找单位信息
     * */
    @GetMapping("/selectTo")
    @ResponseBody
    public TJcFlzddw selectTo(Long  creditCode){

        return tJcFlzddwService.selectByUnitName(creditCode);
    }
//	/**
//	 *查找所有的单位名称
//	 *
//	 */
//	@RequestMapping("/name")
//	public String name( ModelMap mmap){
//		mmap.put("menu",tJcFlzddwService.selectName());
//		return prefix1 + "/tree";
//	}

//	@RequestMapping("/menuTreeData")
//	@ResponseBody
//	public List<Ztree> menuTreeData()
//	{
//		Long userId = ShiroUtils.getUserId();
//		List<Ztree> ztrees = tJcFlzddwService.menuTreeData(userId);
//		return ztrees;
//	}
	@PostMapping("/option")
	@ResponseBody
	public TableDataInfo list(TJcJcjg tJcJcjg)
	{
		startPage();
		List<TJcJcjg> list = tJcFlzddwService.selectTJcJcjgList(tJcJcjg);
		return getDataTable(list);
	}
}
